package cn.zmit.kuxi.holder;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.zmit.kuxi.R;
import cn.zmit.kuxi.activity.GoodsDetailActivity;
import cn.zmit.kuxi.activity.WillAnnounceActivity;
import cn.zmit.kuxi.contants.Url;
import cn.zmit.kuxi.entity.NewWinnerEnity;
import cn.zmit.kuxi.image.ImageDisplayer;

import com.xdroid.functions.holder.ViewHolderBase;

/** * @author chenxu @parameter * @return */
public class NewWinnerHolder extends ViewHolderBase<NewWinnerEnity> {
	private TextView title, goodsName, user, time, luckyNum, join_count;
	private ImageView user_pic;
	private Context context;
	private Button join_next;
	private LinearLayout llRoot;
	private int dTime;

	@SuppressLint("InflateParams")
	@Override
	public View createView(LayoutInflater layoutInflater) {
		context = layoutInflater.getContext();
		View view = layoutInflater.inflate(R.layout.item_winner, null);
		title = (TextView) view.findViewById(R.id.tv_goods_title);
		goodsName = (TextView) view.findViewById(R.id.tv_goods_detail);
		user = (TextView) view.findViewById(R.id.tv_winning_user);
		time = (TextView) view.findViewById(R.id.tv_announced_time);
		luckyNum = (TextView) view.findViewById(R.id.tv_get_winning_num);
		join_count = (TextView) view.findViewById(R.id.tv_join_num);
		user_pic = (ImageView) view.findViewById(R.id.image_item_goods);
		join_next = (Button) view.findViewById(R.id.Btn_join);
		llRoot = (LinearLayout) view.findViewById(R.id.llRoot);
		return view;
	}

	Boolean isAnnouced;

	@Override
	public void showData(int position, final NewWinnerEnity itemData) {
		// TODO Auto-generated method stub
		if (itemData.getUrl() != null && itemData.getUrl().length() > 0) {
			ImageDisplayer.getInstance().dispalyImageWithRadius(
					Url.IMAGE_PICTURES + itemData.getUrl(), user_pic, 0);
		} else {
			user_pic.setImageResource(R.drawable.ic_launcher);
		}

		dTime = itemData.getTime() - itemData.getNowtime();
		if (dTime > 0) {
			isAnnouced = false;
			user.setText("获奖用户:" + "敬请期待");
			luckyNum.setText("获奖号码:" + "敬请期待");
			time.setText("揭晓时间:" + "等待中...");
			join_count.setText("本期参与:" + "计算中");
			Log.e("oldtime==", itemData.getTime() + "");
			Log.e("nowtime==", itemData.getNowtime() + "");
			Log.e("detime==", itemData.getNowtime() - itemData.getTime() + "");
		} else {
			isAnnouced = true;
			user.setText("获奖用户："
					+ itemData.getUser());
			luckyNum.setText("获奖号码:" + itemData.getLuckNum());
			time.setText("揭晓时间:" + itemData.getAnnouncetime());
			join_count.setText("本期参与:" + itemData.getBuy_count() + "次");
		}

		title.setText("第" + itemData.getTitle() + "期");
		goodsName.setText(itemData.getGoodsName());

		join_next.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				Bundle bundle = new Bundle();
				bundle.putString("gp_id", itemData.getNext_gp_id());
				intent.putExtras(bundle);
				intent.setClass(context, GoodsDetailActivity.class);
				context.startActivity(intent);
			}
		});
		llRoot.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				Bundle bundle = new Bundle();
				bundle.putString("gp_id", itemData.getGp_id());
				bundle.putString("next_gp_id", itemData.getNext_gp_id());
				bundle.putString("title", itemData.getTitle());
				bundle.putString("goodsName", itemData.getGoodsName());
				bundle.putBoolean("isAnnounced", isAnnouced);
				intent.putExtras(bundle);
				intent.setClass(context, WillAnnounceActivity.class);
				context.startActivity(intent);
			}
		});

	}

}
